package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetCoorData;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.ListAdapter1;
import com.example.yiwei.crm.view.activity.coordinationActivity.SimpleCoorCustomerActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class SimpleCoordingActivity1 extends BaseActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private ListAdapter1 ladapter;
    private AutoListView lstv;
    List<dockingEntity> dockingList;
    public static SimpleCoordingActivity1 s;
    List<dockingEntity> list;
    private ImageView img_search,img_luru,img_me,img_tj,img_back;
    SharedPreferences sp;
    int page=1;
    int dock_id;
    int a = 1;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<dockingEntity> result = (List<dockingEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    if (dockingList!=null){
                        dockingList.clear();
                    }
                    dockingList.addAll(result);
                    if (a==1){
                        ladapter = new ListAdapter1(SimpleCoordingActivity1.this, dockingList);
                        lstv.setAdapter(ladapter);
                        a++;
                    }
                    break;
                case AutoListView.LOAD:
                    if (result!=null){
                        lstv.onLoadComplete();
                        dockingList.addAll(result);
                    }else {
                        lstv.onLoadComplete();
                    }

            }
//            lstv.setResultSize(result.size());
            ladapter.notifyDataSetChanged();
        }


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_coording1);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        img_search= (ImageView) findViewById(R.id.item_image_cx);
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        lstv = (AutoListView) findViewById(R.id.simple_health_education_listview);
        s=this;
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        lstv.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        lstv.setDividerHeight(1);
        lstv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lstv.setOnRefreshListener(this);
        lstv.setOnLoadListener(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                dockDao dockDao = new dockDaoImpl();
                SC sc = new SC();
                JSONObject object =sc.storage(sp);
                try {
                    object.put("KXA02",getIntent().getStringExtra("KXA02"));
                    object.put("KXA03",getIntent().getStringExtra("KXA03"));
                    object.put("KXA05",getIntent().getStringExtra("KXA05"));
                    object.put("KXA08",getIntent().getStringExtra("KXA08"));
                    object.put("KXA15",getIntent().getStringExtra("KXA15"));
                    object.put("CID03",getIntent().getStringExtra("CID03"));
                    object.put("CTS01A",getIntent().getStringExtra("CTS01A"));
                    object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                dockingList = dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url+1, json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//
                        if (dockingList != null) {
//                            btn_add_visit.setVisibility(View.VISIBLE);
//                            LoadingActivity.l.finish();
                            ladapter = new ListAdapter1(SimpleCoordingActivity1.this, dockingList);
                            lstv.setAdapter(ladapter);
                            ladapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(SimpleCoordingActivity1.this, "该条件下未搜素到信息", Toast.LENGTH_SHORT).show();
                        }
//                        loadData(AutoListView.REFRESH);
                    }
                });
            }
        }).start();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoordingActivity1.this, JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoordingActivity1.this, JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoordingActivity1.this, JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SimpleCoordingActivity1.this, JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                dock_id = dockingList.get(i - 1).getDocking_id();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        dockDao dockDao=new dockDaoImpl();
                        GetDock.create();
                        GetDock.dockingEntity = dockingList.get(i - 1);
                        if (!GetDock.dockingEntity.toString().equals("")) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent(SimpleCoordingActivity1.this,DetailedCoordingActivity.class);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                }).start();
            }
        });
    }

    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
          UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
           LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            SC sc = new SC();
            JSONObject object =sc.storage(sp);
            try {
                object.put("KXA02",getIntent().getStringExtra("KXA02"));
                object.put("KXA03",getIntent().getStringExtra("KXA03"));
                object.put("KXA05",getIntent().getStringExtra("KXA05"));
                object.put("KXA08",getIntent().getStringExtra("KXA08"));
                object.put("KXA15",getIntent().getStringExtra("KXA15"));
                object.put("CID03",getIntent().getStringExtra("CID03"));
                object.put("CTS01A",getIntent().getStringExtra("CTS01A"));
                object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            dockDao dockDao = new dockDaoImpl();
            list= dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url+1, json);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (list!=null){
                if (lstv.getVisibility()==View.GONE){
                    lstv.setVisibility(View.VISIBLE);
                }
                msg.obj = list;
                handler.sendMessage(msg);
            }else {
                lstv.setVisibility(View.GONE);
                Toast.makeText(SimpleCoordingActivity1.this, "无数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            customerDao customerdao = new customerDaoImpl();
            page=page+1;
            SC sc = new SC();
            JSONObject object =sc.storage(sp);
            try {
                object.put("KXA02",getIntent().getStringExtra("KXA02"));
                object.put("KXA03",getIntent().getStringExtra("KXA03"));
                object.put("KXA05",getIntent().getStringExtra("KXA05"));
                object.put("KXA08",getIntent().getStringExtra("KXA08"));
                object.put("KXA15",getIntent().getStringExtra("KXA15"));
                object.put("CID03",getIntent().getStringExtra("CID03"));
                object.put("CTS01A",getIntent().getStringExtra("CTS01A"));
                object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            dockDao dockDao = new dockDaoImpl();
            list= dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url+page, json);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (list!=null){
                msg.obj = list;
                handler.sendMessage(msg);
            }else {
                msg.obj = list;
                handler.sendMessage(msg);
                Toast.makeText(SimpleCoordingActivity1.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetCoorData.get()!=null){
            dockingList = GetCoorData.get();
            ladapter = new ListAdapter1(SimpleCoordingActivity1.this, dockingList);
            lstv.setAdapter(ladapter);
            ladapter.notifyDataSetChanged();
            GetCoorData.dockingEntityList = null;
        }else{
            onRefresh();
        }

    }
}
